﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.Core.ExtensionMethods;

namespace AMS.Core.Math
{
    public class IntEquation : StandardEquation<int>
    {
        public IntEquation(string equation)
        {
            ParseEq(equation);
        }

        private void ParseEq(string equation)
        {
            
            // parse recursively

            // first, check for Parenthesis
            // - if found, find first close paren, then previous open paren
            //   - then, recurse into IntEquation.Parse() for what's contained

            // next, check for multipl operations
            // if multiple operations, then find first priority by PEMDAS
            // once found, take left and right values, remove both and operator, and sub with {#} for SimpleEquation
            // send simple equation to simple equation parser
            // - a simple equation has exactly one operator and two operands; both may be variables

            // next, check for exponents

            // next, check for multiply or divide

            // next, check for addition or substraction
            if (equation.Contains(Add))
            {
                // break into left and right side
                int leftValue = GetLeftValue(equation, equation.IndexOf(Add));
                int rightValue = GetRightValue(equation, equation.IndexOf(Add));
            }
        }

        //private SimpleEquationDelegate<int> ParseSimpleEquation(string simpleEquation)
        //{
            // a simple equation has exactly one operator and two operands
            // Step 1: determine operator
            // Step 2: get value to left of operator (may be a variable)
            // Step 3: get value to righ of operator (may be a variable)
            // Step 4: determine operation associate with operand
            // Step 5: create delegate for operand-operator-operand (simple, OneVariable, TwoVariable)
            //SimpleEquationDelegate<int> simpleEquation = new SimpleEquationDelegate<int>({x,y => x*y})
        //}

        //public delegate T SimpleEquationDelegate<T>(T left, string op, T right);

        private int GetRightValue(string equation, int p)
        {
            int q = p;
            while (q < equation.Length &&
                (Char.IsDigit(equation[q]) || equation[q] == '.' || equation[p] == '-'))
            {
                q++;
            }

            // get substring q to p
            // parse to integer and return

            throw new NotImplementedException();
        }

        private int GetLeftValue(string equation, int p)
        {
            throw new NotImplementedException();
        }

        public static IntEquation Parse(string equation)
        {
            return new IntEquation(equation);
        }

        public override int Solve(int value)
        {
            throw new NotImplementedException();
        }
    }
}
